package com.youmu.sichuansfc.util;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *  时间工具类
 */
public class DateUtil {

    /**
     *  获取当前时间
     * @param type 时间格式 yyyy-MM-dd HH:mm:ss.SSSZ
     * @return
     *         defaulr new Date()
     */
    public static Date getNewTime(String type){
        Calendar now = Calendar.getInstance();

        if (type.isEmpty()){
            return now.getTime();
        }

        SimpleDateFormat fmt = new SimpleDateFormat(type);
        try {
            return fmt.parse(fmt.format(now.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return now.getTime();
    }

    /**
     *  字符串转 时间(Timestamp)
     * @param dateSt
     * @return
     */
    public static Timestamp parseStr(String dateSt){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateSt);
            return new Timestamp(date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转 时间(Date)
     * @param dateSt
     * @param format
     * @return
     */
    public static Date Date_parseStr(String dateSt, String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(dateSt);
            return date;
        } catch (ParseException e) {
            //e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转 时间(Date)
     * @param dateSt
     * @param format
     * @return
     */
    public static String Date_parseStr(Date dateSt, String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            String date = sdf.format(dateSt);
            return date;
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取本月时间段
     * @return Date[0] 本月开始 Date[1]下月开始
     */
    public static Date[] getMonth(){
        Date[] dates = new Date[2];
        Calendar now = Calendar.getInstance();
        return  dates;
    }
    /**
     * 获得当天的起始时间
     * @return
     */
    public static Calendar getStartDate(Calendar today){
        today.set(Calendar.HOUR_OF_DAY,0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        return today;
    }
    /**
     * 获取当天截止时间
     * @return
     */
    public static Calendar getEndDate(Calendar endToday){
        endToday.set(Calendar.HOUR_OF_DAY, 23);
        endToday.set(Calendar.MINUTE, 59);
        endToday.set(Calendar.SECOND, 59);
        endToday.set(Calendar.MILLISECOND, 59);
        return endToday;
    }
    /**
     * 获得当月起始时间
     * @return
     */
    public static Calendar getStartMounth(Calendar today){
        Calendar calendar = getStartDate(today);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        System.out.println(Date_parseStr(calendar.getTime(), "yyyy-MM-dd HH:mm:ss"));
        return calendar;
    }
    /**
     * 获得当月结束时间
     * @return
     */
    public static Calendar getEndMounth(Calendar endToday){
        Calendar endMounth=getEndDate(endToday);
        endMounth.set(Calendar.DAY_OF_MONTH, endMounth.getActualMaximum(endMounth.DAY_OF_MONTH));
        System.out.println(Date_parseStr(endMounth.getTime(), "yyyy-MM-dd HH:mm:ss"));
        return endMounth;
    }


    /**
     * 保送状态: 0 首次; 1 动态 ; 2 月报; 3 季报
     */
    public static  int  getDataType(){
        Calendar calendar = Calendar.getInstance();//日历对象

        //String yearStr = calendar.get(Calendar.YEAR)+"";//获取年份
        int month = calendar.get(Calendar.MONTH) + 1;//获取月份
        int day = calendar.get(Calendar.DATE);//获取日

        // 季度 末
        if (month % 3 == 0){
            if(day >= 16){
                return 3;
            }
        }
        // 月报
        if (day >= 22){
            return 2;
        }
        return 0;
    }
}
